package com.jiang.entity;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

/**
 * 远程端口连接服务
 */
public class RpcServer extends Thread{
    private static Logger LOGGER = LoggerFactory.getLogger(RpcServer.class);
    private int port;
    private boolean runFlag = true;

    public List<RpcThread> rpcThreads;

    public RpcServer() {
        this.port = 8016;
        this.rpcThreads = new ArrayList<RpcThread>();
    }

    public RpcServer(int port) {
        this.port = port;
        this.rpcThreads = new ArrayList<RpcThread>();
    }

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while (runFlag){
                RpcThread rpcThread = new RpcThread(serverSocket.accept());
                rpcThreads.add(rpcThread);
                rpcThread.start();
            }
            serverSocket.close();

        } catch (IOException e) {
            LOGGER.error("rpc error in port"+port, e);
        }
    }
}
